package com.LeeCode;

/**
 * 合并两个链表
 */

public class Code1669 {
    public static void main(String[] args) {
        int[] arr1 = {10, 1, 13, 6, 9, 5};
        int[] arr2 = {1000000, 1000001, 1000002};

        ListNode list1 = Utils.arrayToListNode(arr1);
        ListNode list2 = Utils.arrayToListNode(arr2);

        Utils.printListNode(new Code1669().mergeInBetween(list1, 3, 4, list2));

    }

    public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
        ListNode dummyHead = new ListNode(0, list1);

        ListNode cur = dummyHead;
        for (int i = 0; i < a; i++) {
            cur = cur.next;
        }
        ListNode tail = cur;
        for (int i = 0; i <= b - a + 1; i++) {
            tail = tail.next;
        }
        ListNode tail1 = list2;
        while (tail1.next != null) {
            tail1 = tail1.next;
        }
        cur.next = list2;
        tail1.next = tail;
        return dummyHead.next;
    }
}
